'''
Created on Oct 11, 2012

@author: raygomez
'''
import unittest
from AVP import AVP
from xdrlib import Packer, Unpacker

class Test(unittest.TestCase):

    def testAVP(self):
        a1 = AVP(1, "user")
        self.assertFalse(a1.isMandatory())
        self.assertFalse(a1.isVendorSpecific())
        self.assertFalse(a1.isPrivate())
        self.assertEqual("1", a1.str_prefix__())

        a2 = AVP(1, "user", 2)
        self.assertFalse(a2.isMandatory())
        self.assertTrue(a2.isVendorSpecific())
        self.assertFalse(a2.isPrivate())
        self.assertEqual("1.v:2", a2.str_prefix__())
        
    def testFlags(self):
        a1 = AVP(1, "user")
        a1.setMandatory(True)
        a1.setPrivate(True)
        self.assertEqual("1.m.p", a1.str_prefix__())
        
        a2 = AVP(2, "user")
        a2.setMandatory(False)
        a2.setPrivate(False)
        self.assertEqual("2", a2.str_prefix__())
        
    def testEncode(self):
        a1 = AVP(1, "user")
        size = a1.encodeSize()
        packer = Packer()
        size1 = a1.encode(packer)
        self.assertEqual(size, size1)
        self.assertEqual(12, size)
        
        a2 = AVP(1, "user123", 123)
        size = a2.encodeSize()
        packer = Packer()
        size1 = a2.encode(packer)
        self.assertEqual(size, size1)
        self.assertEqual(20, size)
     
    def testDecode(self):
        toDecode = b'\x00\x00\x00\x01\x00\x00\x00\x0cuser'
        unpacker = Unpacker(toDecode)
        size1 = AVP.decodeSize(unpacker, len(toDecode))
        self.assertEqual(12, size1)

        a1 = AVP()
        result = a1.decode(unpacker, 12)
        self.assertTrue(result)
        self.assertEqual(1, a1.code)
        self.assertEqual(0, a1.vendorId)
        self.assertEqual(0, a1.flags)

        toDecode = b'\x00\x00\x00\x01'
        unpacker = Unpacker(toDecode)
        size1 = AVP.decodeSize(unpacker, len(toDecode))
        self.assertEqual(0, size1)
        
        a1 = AVP()
        result = a1.decode(unpacker, 4)
        self.assertFalse(result)

        toDecode = b'\x00\x00\x00\x01\x80\x00\x00\x0b\x12\x34\x56\x78'
        unpacker = Unpacker(toDecode)
        size1 = AVP.decodeSize(unpacker, len(toDecode))
        self.assertEqual(0, size1)
        
        a1 = AVP()
        result = a1.decode(unpacker, len(toDecode))
        self.assertFalse(result)

        
        toDecode = b'\x00\x00\x00\x01\x00\x00\x00\x07user'
        unpacker = Unpacker(toDecode)
        size1 = AVP.decodeSize(unpacker, len(toDecode))
        self.assertEqual(0, size1)
        
        a1 = AVP()
        result = a1.decode(unpacker, len(toDecode))
        self.assertFalse(result)
        
        toDecode = b'\x00\x00\x00\x01\x80\x00\x00\x11\x12\x34\x56\x78user1\x00\x00\x00'
        unpacker = Unpacker(toDecode)
        size1 = AVP.decodeSize(unpacker, len(toDecode))
        
        a1 = AVP()
        result = a1.decode(unpacker, 20)
        
        self.assertEqual(20, size1)
        self.assertEqual(1, a1.code)
        self.assertEqual(0x12345678, a1.vendorId)
        self.assertTrue(a1.isVendorSpecific())

         
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testAVP']
    unittest.main()